<template>
	<view>
		<view class="mb-2">最近开奖：{{title}}</view>
		<view class="mb-2">
			<view>开奖号码：</view>
			<uni-easyinput v-model="drawResult" placeholder="本期开奖号码" class="my-2"></uni-easyinput>
			<uni-easyinput v-model="prevDrawResult" placeholder="上期开奖号码" class="my-2"></uni-easyinput>
			<uni-data-select v-model="activeIssue" :localdata="historyData" @change="onChangeIssue"
				placeholder="最近 10 期快捷选择"></uni-data-select>
		</view>
		<view class="flex justify-between items-center">
			<view>批量导入号码：</view>
			<view>{{rowsCount}}</view>
		</view>
		<view class="my-2" v-show="importPanelInfo.visible">
			<uni-easyinput type="textarea" v-model="importPanelInfo.content" placeholder="请输入号码，每注号码一行" maxlength="-1"
				autoHeight clearable></uni-easyinput>
		</view>
		<view v-show="!importPanelInfo.visible">
			<view>
				<view class="flex items-center my-2" v-for="(item, idx) in data" :key="idx">
					<view>#{{ idx + 1 }}</view>
					<view class="mx-2" v-html="item.text"></view>
					<view :class="item.money > 0 ? 'text-red-400' : ''">
						{{ item.money > 0 ? (item.frontCount + ' + ' + item.backCount) : '----' }}
					</view>
					<view class="text-error ms-4">{{item.statusText + (item.status>0 ? `（${item.moneyText}元）` : '')}}
					</view>
				</view>
			</view>
		</view>
		<view class="mt-2" v-show="!importPanelInfo.visible">
			<button size="mini" type="primary" @click="importPanelInfo.visible = true">编辑号码</button>
			<button class="mx-1" size="mini" type="info" @click="onCopy()">复制</button>
		</view>
		<view class="mt-2" v-show="importPanelInfo.visible">
			<view>
				<button size="mini" type="primary" @click="onCalc()">立即计算</button>
				<button class="mx-1" size="mini" @click="importPanelInfo.content = ''">清空</button>
			</view>
			<view>
				<button size="mini" @click="onReadTemp()">读取暂存</button>
				<button class="mx-1" size="mini" @click="onClearTemp()">删除暂存</button>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import DltOrderModel from '@/model/DltOrderModel';
	import DltDataModel from '@/model/DltDataModel';
	import FsDriver from '@/model/FsDriver';
	import {
		copyText
	} from '@/utils/Utils';

	const fsDriver = new FsDriver();
	const dltDataModel = new DltDataModel();
	const dltOrderModel = new DltOrderModel();
	const dataset = dltDataModel.getDataSource().slice(0, 10);
	const weekdays = ['日', '一', '二', '三', '四', '五', '六'];
	const historyData = dataset.map((v, i) => {
		return {
			text: `${v.lotteryDrawNum}（${v.lotteryDrawTime.substring(5)} ${weekdays[new Date(v.lotteryDrawTime).getDay()]}）${v.lotteryDrawResult}`,
			value: i
		};
	});
	const title = ref('');
	if (dataset.length > 0) {
		for(let v of dataset){
			title.value = `${v.lotteryDrawNum}（${v.lotteryDrawTime.substring(5)}/周${weekdays[new Date(v.lotteryDrawTime).getDay()]}）${v.lotteryDrawResult}`;
			break;
		}
	}
	const drawResult = ref('');
	const prevDrawResult = ref('');
	const activeIssue = ref('');
	const importPanelInfo = ref({
		visible: true,
		content: ''
	})
	const rowTpl = {
		order: '',
		text: '',
		frontCount: 0,
		backCount: 0,
		totalCount: 0,
		statusText: '',
		status: '',
		moneyText: '',
		money: 0,
		sum: 0,
		diffSum: 0,
	};
	const data = ref([rowTpl]);
	const rowsCount = ref(0);

	const onChangeIssue = (idx) => {
		drawResult.value = dataset[idx].lotteryDrawResult;
		prevDrawResult.value = dataset[idx + 1].lotteryDrawResult;
	}

	const onCalc = () => {
		if (drawResult.value.trim().length == 0) {
			uni.showToast({
				title: '开奖号码无效',
				icon: 'error'
			})
			return;
		}
		if (importPanelInfo.value.content.trim().length == 0) {
			uni.showToast({
				title: '投注号码无效',
				icon: 'error'
			})
			return;
		}

		data.value = [];
		importPanelInfo.value.visible = false;
		let orderList = data.value.filter(v => v.order.length > 0).map(v => v.order);
		importPanelInfo.value.content.trim().split('\n').forEach(v => {
			v = v.replace(/\,|"/g, ' ').trim();
			if (v.length == 0 || v.split(' ').length != 7 || orderList.includes(v)) {
				return;
			}
			orderList.push(v);
			data.value.push({
				...rowTpl,
				order: v,
				text: v
			})
		})
		if (data.value[0].order == '') {
			data.value = data.value.slice(1);
		}
		data.value = dltOrderModel.calcOrder(drawResult.value, orderList, false, prevDrawResult.value);
		rowsCount.value = orderList.length;
	}

	const onCopy = () => {
		let arr = []
		data.value.forEach(item => {
			if (item.money > 0) {
				arr.push(
					`${item.order} ${item.statusText}【${item.moneyText}元】【${item.frontCount}+${item.backCount}】`
				)
			} else {
				arr.push(`${item.order} ${item.statusText}`)
			}
		})
		copyText(arr.join('\n'));
		uni.showToast({
			title: '复制成功',
			icon: 'success'
		})
	}

	const onReadTemp = () => {
		let dataStr = fsDriver.get('temp-order');
		if (!dataStr) {
			uni.showToast({
				title: '没有暂存的选号数据',
				icon: 'error'
			})
			return;
		}
		let data = JSON.parse(dataStr);
		rowsCount.value = data.length;
		importPanelInfo.value.content = data.join('\n');
	}

	const onClearTemp = () => {
		uni.showModal({
			title: '提示',
			content: '确定要删除暂存的选号内容吗？',
			success: function(res) {
				if (res.confirm) {
					fsDriver.save('temp-order', '');
					rowsCount.value = 0;
				}
			}
		});
	}
</script>